package com.kaifamiao.tree;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapTest3 {

	public static void main(String[] args) {
		
		// 使用匿名类实现java.util.Comparator接口并完成对Product实例的比较
		Comparator<Product> comparator = new Comparator<>() {
			@Override
			public int compare(Product a, Product b) {
				System.out.println( "比较" );
				if( a != null && a.getId() != null && b!= null && b.getId() != null ) {
					return a.getId() - b.getId() ;
				}
				return 0;
			}
		};
		
		// 构造一个新的、空的树映射，该映射根据给定比较器进行排序
		TreeMap<Product,Integer> map = new TreeMap<>( comparator );
		
		Product p1 = new Product( 1 , "老干妈" , 1200 , 90 , "瓶" );
		map.put( p1 , 10 );
		System.out.println( map );
		
		Product p2 = new Product( 1 , "老干妈" , 1200 , 90 , "瓶" );
		map.put( p2 , 20 );
		System.out.println( map );
		
		map.put( p2 , null );
		System.out.println( map );

	}

}
